package cn.zwk.coupon.service.impl;

import cn.zwk.common.to.SkuReductionTo;
import cn.zwk.coupon.entity.MemberPriceEntity;
import cn.zwk.coupon.entity.SkuLadderEntity;
import cn.zwk.coupon.service.MemberPriceService;
import cn.zwk.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zwk.common.utils.PageUtils;
import cn.zwk.common.utils.Query;

import cn.zwk.coupon.dao.SkuFullReductionDao;
import cn.zwk.coupon.entity.SkuFullReductionEntity;
import cn.zwk.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Autowired
    private MemberPriceService memberPriceService;
    @Autowired
    private SkuLadderService skuLadderService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo reductionTo) {
        if (reductionTo.getFullCount()>0){
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            skuLadderEntity.setSkuId(reductionTo.getSkuId());
            skuLadderEntity.setFullCount(reductionTo.getFullCount());
            skuLadderEntity.setDiscount(reductionTo.getDiscount());
            skuLadderEntity.setAddOther(reductionTo.getCountStatus());
            skuLadderService.save(skuLadderEntity);
        }

        if (reductionTo.getFullPrice().compareTo(BigDecimal.ZERO)>0){
            SkuFullReductionEntity entity = new SkuFullReductionEntity();
            entity.setSkuId(reductionTo.getSkuId());
            entity.setFullPrice(reductionTo.getFullPrice());
            entity.setReducePrice(reductionTo.getReducePrice());
            entity.setAddOther(reductionTo.getCountStatus());
            this.save(entity);
        }
        if (reductionTo.getMemberPrice()!=null&& !reductionTo.getMemberPrice().isEmpty()) {
            List<MemberPriceEntity> memberPriceEntities = reductionTo.getMemberPrice().stream().map(item -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setMemberLevelId(item.getId());
                memberPriceEntity.setMemberLevelName(item.getName());
                memberPriceEntity.setMemberPrice(item.getPrice());
                memberPriceEntity.setSkuId(reductionTo.getSkuId());
                return memberPriceEntity;
            }).filter(item->item.getMemberPrice().compareTo(BigDecimal.ZERO)>0).collect(Collectors.toList());
            memberPriceService.saveBatch(memberPriceEntities);
        }
    }
}
